package com.example.qcweb;

import android.content.Context;

import com.example.dao.DBDao;
import com.example.modal.CardInfo;
import com.example.modal.DataModal;
import com.example.modal.ExamineItem;
import com.example.modal.FormFieldList;
import com.example.modal.MostPortItem;
import com.example.modal.MultimediaDemand;
import com.example.modal.MultimediaRecord;
import com.example.modal.QcCard;
import com.example.modal.User;
import com.example.modal.WorkingProcedure;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class DataManager {

    public static List<Map<String, Object>> cardSource() {
        List<Map<String, Object>> dataSource = new ArrayList<Map<String, Object>>();

        return dataSource;
    }

    public static List<String> getParseFileNameList() {
        //List<String> NeedParsefileList=new ArrayList<String>();
        List<String> WilldeleteFileList = new ArrayList<String>();
        String cardPath = DirManager.getXmlSourcePath() + "/";
        List<String> sourceFilenameList = DataManager.getAllFileName(cardPath);
        List<String> savedFileList = MyGlobal.getSavedFile();
        if (savedFileList != null) {
            for (int i = 0; i < sourceFilenameList.size(); i++) {
                String filename = sourceFilenameList.get(i);

                for (int j = 0; j < savedFileList.size(); j++) {
                    if (savedFileList.get(j).equals(filename)) {
                        WilldeleteFileList.add(filename);
                    }
                }


            }
        }

        for (int m = 0; m < WilldeleteFileList.size(); m++) {
            sourceFilenameList.remove(WilldeleteFileList.get(m));
        }
        return sourceFilenameList;
    }

    public static void InitTables(Context context) {
        // TODO Auto-generated method stub

        String cardPath = DirManager.getXmlSourcePath() + "/";
        List<String> filenameArray = getParseFileNameList();
        for (int i = 0; i < filenameArray.size(); i++) {

            DataOutputStream osStream = null;
            Process process = null;
            String cmddata = "chmod 777 /data/data/com.example.qc/XmlSource/" + filenameArray.get(i);
            try {
                process = Runtime.getRuntime().exec("su");
                osStream = new DataOutputStream(process.getOutputStream());
                osStream.writeBytes(cmddata + "\n");
                osStream.writeBytes("exit\n");
                osStream.flush();
                try {
                    process.waitFor();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                try {
                    if (osStream != null) {
                        osStream.close();
                    }
                    process.destroy();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            File xmlFile = new File(cardPath + filenameArray.get(i));
            DataModal dataModal = GetCardSource(xmlFile);
            QcCard qcCard = dataModal.getQcCard();
            qcCard.setKEYCardCode(qcCard.getCardCode());
            qcCard.setLastModifyDate(DateManager.getModifyDateString());
            CardInfo cardInfo = new CardInfo(filenameArray.get(i), DateManager.getModifyDateString(), qcCard.getKEYCardCode(), qcCard.getDrawingNumber(), qcCard.getProductName(), qcCard.getProductModel(), qcCard.getProductBatchNumber(), qcCard.getSystemId());
            List<WorkingProcedure> wplist = dataModal.getWorkingProcedureList();
            List<ExamineItem> examineItems = dataModal.getExamineItemList();
            List<MostPortItem> mostPortItems = dataModal.getMostPortItemList();
            List<MultimediaDemand> multimediaDemands = dataModal.getMultimediaDemandList();
            List<MultimediaRecord> multimediaRecords = dataModal.getMultimediaRecordList();
            List<FormFieldList> formFieldLists = dataModal.getFormFieldList();
            DBDao dbDao = new DBDao(context);
            int lenth = wplist.size();
            for (WorkingProcedure wp : wplist) {
                wp.setKEYCardCode(qcCard.getCardCode());
                wp.setNeedForm(";1-1;13-1;15-1;");
                try {
                    dbDao.insert(wp, context);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for (ExamineItem exitem : examineItems) {
                try {
                    exitem.setKEYCardCode(qcCard.getCardCode());
                    dbDao.insert(exitem, context);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for (FormFieldList formFieldList : formFieldLists) {
                try {
                    formFieldList.setKEYCardCode(qcCard.getCardCode());
                    dbDao.insert(formFieldList, context);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for (MultimediaDemand mDemand : multimediaDemands) {
                try {
                    mDemand.setKEYCardCode(qcCard.getCardCode());
                    dbDao.insert(mDemand, context);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for (MostPortItem mPortItem : mostPortItems) {
                try {
                    mPortItem.setKEYCardCode(qcCard.getCardCode());
                    dbDao.insert(mPortItem, context);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for (MultimediaRecord mRecord : multimediaRecords) {
                try {
                    mRecord.setKEYCardCode(qcCard.getCardCode());
                    dbDao.insert(mRecord, context);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            try {
                dbDao.insert(cardInfo, context);
                dbDao.insert(qcCard, context);

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //��ȡ�ʿؿ��ļ����б�
    public static List<String> getAllFileName(String path) {
        List<String> FileNameArray = new ArrayList<String>();
        File file = new File(path);
        File[] files = file.listFiles();
//		Process process;
//		process=Runtime.getRuntime().exec("chmod 777 "+path+);
        if (files != null) {

            for (int i = 0; i < files.length; i++) {
                if (files[i].getName().endsWith(".xml")) {
                    FileNameArray.add(files[i].getName());

                }
            }

        }
        return FileNameArray;

    }

    private static DataModal GetCardSource(File xmlFile) {
        // TODO Auto-generated method stub

        InputStream instream = null;
        try {
            instream = new FileInputStream(xmlFile);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        XmlParse xmlParse = new XmlParse();
        DataModal dataModal = (DataModal) xmlParse.getXmlObject2(instream, DataModal.class);
        return dataModal;
    }

    public static void InitUser(Context context) {
        // TODO Auto-generated method stub
        DBDao dbDao = new DBDao(context);
        String userpath = DirManager.getXmlUserPath() + "/user.xml";
        File file = new File(userpath);
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        XmlParse xmlParse = new XmlParse();
        List<User> userList = (List<User>) xmlParse.getXmlList(is, User.class, "user");
        for (User user : userList)
            try {
                dbDao.insert(user, context);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    public static void imPortAllXmlFile(Context context) {
        // TODO Auto-generated method stub
        MyGlobal.removeAllSavedFile();
        DropAllData(context);
        InitTables(context);
    }

    private static void DropAllData(Context context) {
        // TODO Auto-generated method stub
        DBDao dbDao = new DBDao(context);
        dbDao.clearTable("CardInfo");
        dbDao.clearTable("ExamineItem");
        dbDao.clearTable("FormFieldList");
        dbDao.clearTable("Item");
        dbDao.clearTable("MostPortItem");
        dbDao.clearTable("MultimediaDemand");
        dbDao.clearTable("MultimediaRecord");
        dbDao.clearTable("QcCard");
        dbDao.clearTable("RecordingProductList");
        dbDao.clearTable("WorkingProcedure");
    }

    public static void imPortNewXmlFile(Context context) {
        // TODO Auto-generated method stub
        DBDao dbDao = new DBDao(context);
        List<CardInfo> cardInfos = dbDao.findAllCardInfo();
        for (int i = 0; i < cardInfos.size(); i++) {
            String filenameString = cardInfos.get(i).getFileName();
            MyGlobal.addSavedFile(filenameString);
        }
        ;
        InitTables(context);

    }


}
